define(function(require) {

  function trans2Path(points, type = 'broken', scale = 1, radius = 50) {
    let pathString = `M ${points[0][0] * scale} ${points[0][1] * scale}`
    const len = points.length;
    let i = 0;
  
    switch (type) {
      case 'broken':
        for (i = 1; i < len; i++) {
          pathString += ` L ${points[i][0] * scale} ${points[i][1] * scale}`
        }
        break;
      case 'brokenArc':
        for (i = 1; i < len - 1; i++) {
          const A = [points[i - 1][0] * scale, points[i - 1][1] * scale]
          const O = [points[i][0] * scale, points[i][1] * scale]
          const B = [points[i + 1][0] * scale, points[i + 1][1] * scale]
  
          const vecOA = [A[0] - O[0], A[1] - O[1]]
          const vecOB = [B[0] - O[0], B[1] - O[1]]
  
          const dotOAOB = vecOA[0] * vecOB[0] + vecOA[1] * vecOB[1]
          const lenOA = Math.sqrt(vecOA[0] * vecOA[0] + vecOA[1] * vecOA[1])
          const lenOB = Math.sqrt(vecOB[0] * vecOB[0] + vecOB[1] * vecOB[1])
  
          const degAOBcos = dotOAOB / (lenOA * lenOB)
          const degAOB = Math.acos(degAOBcos)
          const degAOCtan = Math.tan(degAOB / 2)
  
          const testCutLen = Math.min(lenOA, lenOB) / 2; //假设取两条边中较短的一根的一半做为切点
          const testRadius = testCutLen * degAOCtan; //解此时可以支撑的圆弧半径
          
          let cutLen = testCutLen;
          let realRadius = radius * scale;
          if (testRadius <= realRadius) {
            realRadius = testRadius
          } else {
            cutLen = realRadius / degAOCtan
          }
  
          const vecOAUnit = [vecOA[0] / lenOA, vecOA[1] / lenOA]
          const vecOBUnit = [vecOB[0] / lenOB, vecOB[1] / lenOB]
  
          const A0 = [O[0] + cutLen * vecOAUnit[0], O[1] + cutLen * vecOAUnit[1]]
          const B0 = [O[0] + cutLen * vecOBUnit[0], O[1] + cutLen * vecOBUnit[1]]
          const crossZ = vecOA[0] * vecOB[1] - vecOA[1] * vecOB[0]
          if (crossZ < 0) {
            pathString += ` L ${A0[0]} ${A0[1]} A ${realRadius} ${realRadius} 0 0 1 ${B0[0]} ${B0[1]}`
          } else if (crossZ > 0) {
            pathString += ` L ${A0[0]} ${A0[1]} A ${realRadius} ${realRadius} 0 0 0 ${B0[0]} ${B0[1]}`
          } else {
            pathString += ` L ${O[0]} ${O[1]}`
          }
        }
  
        pathString += ` L ${points[i][0] * scale} ${points[i][1] * scale}`
        
        break;
      case 'bezier':
        for (i = 1; i < len - 1; i++) {
          pathString += ` Q ${points[i][0] * scale} ${points[i][1] * scale} ${points[i + 1][0] * scale} ${points[i + 1][1] * scale}`
        }
        break
      default:
        break;
    }
  
    return {
      pathString: pathString
    }
  }
  
  function createSvgPathElement(pathString) {
    var pathElement = document.createElementNS('http://www.w3.org/2000/svg', 'path')
    pathElement.setAttribute('d', pathString)
    return pathElement;
  }

  return { trans2Path, createSvgPathElement }
})